package cn.lx.redis.utils;


import org.apache.commons.lang3.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Tuple;
import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.util.SafeEncoder;

import java.util.*;


public abstract class AbstractRedisUtil
{



    // 获取jedis连接
    protected abstract Jedis getJedis()  throws NullPointerException;

    // 释放jedis连接
    protected abstract void returnResource(Jedis jedis);
    /**
     * -------------------String开始----------------------------
     */
    /**
     * String类型设置值  key value
     * @param key
     * @param value
     * @return
     */
    public boolean set(String key, String value)
    {
        Jedis jedis = null;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(key) && StringUtils.isNotBlank(value))
            {
                jedis.set(key, value);
                return true;
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();

        }
        finally
        {
            returnResource(jedis);
        }
        return false;
    }

    /**
     * 获取String 类型key的值
     * @param key
     * @return
     */
    public String get(String key)
    {
        String result = null;
        Jedis jedis = null;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(key))
            {
                result = jedis.get(key);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
        }
        return result;
    }

    /**
     * 删除String 类型 key
     * @param key
     * @return
     */
    public boolean del(String key)
    {
        Jedis jedis = null;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(key))
            {
                jedis.del(key);
                return true;
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
        }
        return false;
    }

    /**
     * 增加String 类型 key d的值
     * @param key
     * @param count
     * @return
     */
    public  Long incrBy(String key, Long count) {
        boolean broken = false;
        Long result = null;
        Jedis jedis = null;
        try {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(key)) {
                result = jedis.incrBy(key, count);
            }
        } catch (JedisConnectionException var2) {
            broken = true;

            throw var2;
        } finally {
            returnResource(jedis);
        }
        return result;
    }

    /**
     * 减少String类型key 的值
     * @param key
     * @param count
     * @return
     */
    public  Long decrBy(String key, Long count) {
        boolean broken = false;
        Long result = null;
        Jedis jedis = null;
        try {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(key)) {
                result = jedis.decrBy(key, count);
            }
        } catch (JedisConnectionException var2) {
            broken = true;

            throw var2;
        }finally {
            returnResource(jedis);
        }
        return result;
    }
    /**
     * 将 key 中储存的数字值增一
     */
    public long incr(String key)
    {
        Jedis jedis = null;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(key))
            {
                return jedis.incr(key);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
        }
        return 0;
    }

    /**
     * 将key 中储存的数字值增N
     */
    public long incrBy(String key, int count)
    {
        Jedis jedis = null;
        try
        {
            jedis = getJedis();
            return jedis.incrBy(key, count);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
        }
        return 0;
    }

    /**
     * 将 key 中储存的数字值减一
     */
    public long decr(String key)
    {
        Jedis jedis = null;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(key))
            {
                return jedis.decr(key);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
        }
        return 0;
    }

    /**
     * 将key 中储存的数字值减N
     */
    public long decrBy(String key, int count)
    {
        Jedis jedis = null;
        try
        {
            jedis = getJedis();
            return jedis.decrBy(key, count);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
        }
        return 0;
    }

    /**
     * key值尾部追加value
     * @param key
     * @param value
     * @return
     */
    public boolean append (String key ,String value){
        Jedis jedis = null;
        try{
            jedis = getJedis();
            Long length = null;
            boolean flag = false;
            if(StringUtils.isNotBlank(key) && StringUtils.isNotBlank(value)){
                //返回字符长度
                length= jedis.append(key, value);
            }
            return length != null;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }finally {
            returnResource(jedis);
        }
    }

    /**
     * 获取String类型key 值的长度
     * @param key
     * @return
     */
    public Long strlen(String key){
        Jedis jedis = null;
        Long strlen = 0l;
        try{
            jedis = getJedis();
            if(StringUtils.isNotBlank(key) ){
                strlen = jedis.strlen(key);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            returnResource(jedis);
            return strlen;
        }
    }
    /**
     * -------------------String结束---------------------------
     */


    /**
     * -------------------Hash散列类型----------------------------
     */

    /**
     * 设置Hash散列
     * @param keyGroup   字段集合
     * @param key    字段
     * @param value   值
     * @return
     */
    public Long hset(String keyGroup, String key, String value)
    {
        Jedis jedis = null;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(keyGroup) && StringUtils.isNotEmpty(key)&& StringUtils.isNotBlank(value))
            {
                return jedis.hset(keyGroup, key, value);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
        }
        return -1L;
    }

    /**
     * hmset同时设置多个字段的值
     * @param keyGroup
     * @param keys
     * @param values
     * @return
     */
    public String hmset(String keyGroup, String[] keys, String[] values)
    {
        Jedis jedis = null;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(keyGroup) && (keys != null && keys.length > 0 ) && (values != null && values.length > 0 )  && keys.length == values.length)
            {
                Map<String, String> map = null;
                if(jedis.exists(keyGroup))
                {
                   map = jedis.hgetAll(keyGroup);
                }
                else
                {
                    map = new HashMap();
                }
                for(int i= 0 ; i < keys.length ;i++){
                    map.put(keys[i], values[i]);
                }
                return jedis.hmset(keyGroup, map);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
        }
        return null;
    }

    /**
     * 获取多个key的值
     * @param keyGroup
     * @param keys
     * @return
     */
    public Map<String, String> hmget(String keyGroup, String... keys)
    {
        Jedis jedis = null;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(keyGroup) && keys != null && keys.length > 0)
            {
                Map<String, String> map = new HashMap();
                List<String> list = jedis.hmget(keyGroup, keys);
                for (int i = 0; i < keys.length; i++)
                {
                    map.put(keys[i], list.get(i));
                }
                return map;
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
        }
        return null;
    }

    /**
     *获取单个key的值
     * @param keyGroup
     * @param key
     * @return
     */
    public String hget(String keyGroup, String key)
    {
        Jedis jedis = null;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(keyGroup) && StringUtils.isNotEmpty(key))
            {
                return jedis.hget(keyGroup, key);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
        }
        return null;
    }

    /**
     * 删除Hash 中的字段
     * @param keyGroup
     * @param keys
     */
    public boolean hdel(String keyGroup, String... keys) {
        // TODO Auto-generated method stub
        Jedis jedis = null;
        boolean flag = true;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(keyGroup) && keys.length>0)
            {
                jedis.hdel(keyGroup, keys);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            flag = false;
        }
        finally
        {
            returnResource(jedis);
            return flag;
        }
    }
    /**
     * 修改key 的value
     * @param key
     * @return
     */
    public Long hincrby(String keyGroup, String key, int value)
    {
        Jedis jedis = null;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(keyGroup) && StringUtils.isNotEmpty(key))
            {
                return jedis.hincrBy(keyGroup, key, value);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
        }
        return -1L;
    }

    /**
     * 获取Hash 中所有的字段值
     * @param keyGroup
     * @return
     */
    public  List<String> hvals(String keyGroup){
        Jedis jedis = null;
        List<String> list = null;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(keyGroup))
            {
                list = jedis.hvals(keyGroup);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
            return list;
        }

    }

    /**
     * 获取Hash  keygroup中所有的Key
     * @param keyGroup
     * @return
     */
    public  Set<String> hkeys(String keyGroup){
        Jedis jedis = null;
        Set<String> hkeys = null;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(keyGroup))
            {
                hkeys = jedis.hkeys(keyGroup);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
            return hkeys;
        }
    }

    /**
     * 获取Hash 字段的数量
     * @param keyGroup
     * @return
     */
    public Long hlen(String keyGroup){
        Jedis jedis = null;
        Long hlen = null;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(keyGroup))
            {
                hlen = jedis.hlen(keyGroup);

            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
            return hlen;
        }
    }

    /**
     * 获取Hash 中key值是否存在
     * @param keyGroup
     * @return
     */
    public boolean hexists(String keyGroup,String key){
        Jedis jedis = null;
        Boolean flag = false;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(keyGroup) && StringUtils.isNotBlank(key))
            {
                flag = jedis.hexists(keyGroup, key);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
            return flag;
        }
    }


    /**
     *------------------Hash类型结束-------------------------------
     */


    /**
     *------------------Set类型开始-------------------------------
     */


    /**
     * Set中添加值
     * @param key
     * @param values
     * @return
     */

    public boolean sadd(String key, String... values)
    {
        Jedis jedis = null;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(key) && values != null && values.length>0 )
            {
                jedis.sadd(key, values);

                return true;
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
        }
        return false;
    }

    /**
     * 获取Set集合中元素的个数
     * @param key
     * @return
     */
    public Long scard(String key)
    {
        Jedis jedis = null;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(key))
            {
                return jedis.scard(key);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
        }
        return 0L;
    }

    /**
     * 判断key集合中的memebr是否存在
     * @param key
     * @param member
     * @return
     */
    public boolean sismember(String key, String member)
    {
        Jedis jedis = null;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(key) && StringUtils.isNotEmpty(member))
            {
                return jedis.sismember(key, member);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
        }
        return false;
    }

    /**
     * 获取集合中所有的值
     * @param key
     * @return
     */
    public Set<String> smembers(String key)
    {
        Jedis jedis = null;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(key))
            {
                return jedis.smembers(key);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
        }
        return null;
    }

    /**
     * 判断集合中某个value是否存在
     * @param key
     * @param value
     * @return
     */
    public Boolean sismembrs(String key,String value){
        Jedis jedis = null;
        Boolean flag = false;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(key) && StringUtils.isNotBlank(value))
            {
                flag = jedis.sismember(key, value);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
        }
        return flag;
    }

    /**
     * 从集合中随机获取一个元素
     * @param key
     * @return
     */
    public String spop(String key)
    {
        Jedis jedis = null;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(key))
            {
                return jedis.spop(key);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
        }
        return null;
    }

    /**
     * 从集合中移除值
     * @param key
     * @param values
     * @return
     */
    public boolean srem(String key,String... values){

        Jedis jedis = null;
        Long val = 0L;

        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(key) && values != null && values.length>0)
            {
                val = jedis.srem(key, values);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
        }
        return !val .equals(0L);
    }


    /**
     * ---------------Set类型结束-----------------------------
     */

    /**
     * ---------------List类型开始-----------------------------
     */


    /**
     * 向左边添加元素
     * @param key
     * @param values
     */
    public Long lpush(String key, String... values)
    {
        Jedis jedis = null;
        Long lpush = null;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(key))
            {
                lpush = jedis.lpush(key, values);

            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
        }
        return lpush;
    }
    /**
     * 向右边添加元素
     * @param key
     * @param values
     */
    public Long rpush(String key, String... values)
    {
        Jedis jedis = null;
        Long rpush = null;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(key))
            {
                rpush = jedis.rpush(key, values);

            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
        }
        return rpush;
    }

    /**
     * 返回列表 key 中指定区间内的元素，区间以偏移量 start 和 stop 指定
     *start 是开始索引 ，stop是结束索引，如果stop是负数，则表示从右边开始计算。左边索引从0开始，右边索引从-1开始
     * @param key
     * @param start
     * @param stop
     * @return
     */
    public List<String> lrange(String key, int start, int stop)
    {
        Jedis jedis = null;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(key))
            {
                return jedis.lrange(key, start, stop);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
        }
        return null;
    }

    /**
     * 从左边弹出第一个元素
     * @param key
     * @return
     */
    public String lpop(String key)
    {
        Jedis jedis = null;
        String lpop = null;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(key))
            {
                lpop = jedis.lpop(key);

            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
        }
        return lpop;
    }
    /**
     * 从右边弹出第一个元素
     * @param key
     * @return
     */
    public String rpop(String key)
    {
        Jedis jedis = null;
        String rpop = null;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(key))
            {
                rpop = jedis.rpop(key);

            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
        }
        return rpop;
    }

    /**
     * 获取集合的长度
     * @param key
     * @return
     */
    public Long llen(String key){
        Jedis jedis = null;
        Long llen = null;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(key))
            {
                llen = jedis.llen(key);

            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
        }
        return llen;
    }

    /**
     * 删除列表中前count个值为value的元素。如果count大于0从左边开始删除，小于0从右边开始删除，等于0 删除所有值为value的元素
     * @param key
     * @param count
     * @param value
     * @return
     */
    public Boolean lrem(String key,Long count,String value){
        Jedis jedis = null;
        Long lrem = 0L;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(key) && StringUtils.isNotBlank(value) && count != null)
            {

                lrem = jedis.lrem(key, count, value);

            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
        }
        return !lrem.equals(0L);
    }

    /**
     * 获取指定所有的值
     * @param key
     * @param index
     * @return
     */
    public String lIndex(String key,Long index){
        Jedis jedis = null;
        String lindex = null;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(key) && index != null)
            {

                lindex = jedis.lindex(key, index);

            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
        }
        return lindex;
    }

    /**
     * 设置某个索引的值
     * @param key
     * @param index
     * @param value
     * @return
     */
    public String lSet(String key,Long index,String value){
        Jedis jedis = null;
        String lset = null;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(key) && index != null && StringUtils.isNotBlank(value))
            {

                lset = jedis.lset(key, index, value);

            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
        }
        return lset;
    }

    /**
     * 从集合中随机获取值
     * @param key
     * @return
     */
    public String srandmember(String key)
    {
        Jedis jedis = null;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(key))
            {
                return jedis.srandmember(key);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
        }
        return null;
    }
    /**
     * --------------------List类型结束-----------------------------
     */

    /**
     * --------------------SortedSet类型结束-----------------------------
     */


    /**
     * 单个插入有序集合
     *
     * @param score
     * @param key
     * @param memeber
     * @return
     */
    public  Long zadd(String key,Double score,String memeber ) throws Exception {
        boolean broken = false;
        Long zadd = 0L;
        Jedis jedis = null;
        try {
            jedis = getJedis();
            zadd = jedis.zadd(key, score, memeber);
        } catch (Exception e) {
          e.printStackTrace();
        } finally {
            returnResource(jedis);
        }
        return zadd;
    }

    /**
     * 批量插入有序集合
     *
     * @param key
     * @param map
     * @return
     */
    public  Long zadd(String key, Map<String, Double> map) throws Exception {
        boolean broken = false;
        Long result = 0L;
        Jedis jedis = null;
        try {
            jedis = getJedis();
            if (!map.isEmpty()){
                result = jedis.zadd(key, map);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            returnResource(jedis);
        }
        return result;
    }

    /**
     * 获取memebr的分数
     * @param key
     * @param member
     * @return
     */
    public Double zscore(String key ,String member){
        Jedis jedis = null;
        Double zscore = null;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(key) && StringUtils.isNotBlank(member))
            {
                zscore = jedis.zscore(key, member);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
        }
        return zscore;
    }

    /**
     *删除元素的值
     * @param key
     * @param members
     * @return
     */
    public Boolean zrem(String key ,String... members){
        Jedis jedis = null;
        Long zrem = 0L;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(key) && members != null && members.length>0)
            {
                zrem = jedis.zrem(key, members);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
        }
        return !zrem.equals(0L);
    }

    /**
     * zrange key sart stop 按照分数从小到大顺序返回索引之间的元素，包含两端的元素
     * @param key
     * @param start
     * @param end
     * @return
     */
    public Set<String> zrange(String key ,Long start,Long end){
        Jedis jedis = null;
        Set<String> zrange = null;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(key) && start != null && end != null)
            {
                zrange = jedis.zrange(key, start, end);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
        }
        return zrange;
    }

    /**
     * zrevrange key start stop按照分数从大到小的顺序，返回元素,同时返回对应的分数
     * @param key
     * @param start
     * @param end
     * @return
     */
    public Set<Tuple> zrevrange(String key ,Long start,Long end){
        Jedis jedis = null;
        Set<Tuple> tuples = null;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(key) && start != null && end != null)
            {

                tuples = jedis.zrangeByScoreWithScores(key, start, end);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
        }
        return tuples;
    }
    /**
     * 获取key对应list的大小
     *
     * @param key
     * @return
     */
    public  int zcard(String key) throws Exception {
        boolean broken = false;
        int result = 0;
        Jedis jedis = null;
        try {
            jedis = getJedis();
            result = jedis.zcard(SafeEncoder.encode(key)).intValue();
        } catch (Exception e) {
            broken = true;

            throw e;
        } finally {
            returnResource(jedis);
        }
        return result;
    }

    /**
     * 修改某个元素的分数
     * @param key
     * @param score
     * @param member
     * @return
     */
    public Double zincrby(String key ,Double score,String member){
        Jedis jedis = null;
        Double zincrby = null;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(key) && score != null && StringUtils.isNotBlank(member))
            {

                zincrby = jedis.zincrby(key, score, member);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
        }
        return zincrby;
    }

    /**
     * 获取某个分数段的元素个数
     * @param key
     * @param minScore
     * @param maxScoure
     * @return
     */
    public Long zcount(String key ,Double minScore,Double maxScoure){
        Jedis jedis = null;
        Long zcount = null;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(key) && minScore != null && maxScoure != null)
            {

                zcount = jedis.zcount(key, minScore, maxScoure);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
        }
        return zcount;
    }


    /**
     *---------------SortedSet结束--------------------
     */

    /**
     *---------------key操作--------------------
     */

    /**
     *获取key的生存时间
     * @param key
     * @return
     */
    public Long ttl(String key) {

        Jedis jedis = null;
        Long result = null;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(key))
            {

                result = jedis.ttl(key);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
        }

        return result;
    }

    /**
     * 设置Key的生存时间
     * @param key
     * @param halfhourtimeout
     */
    public void expire(String key, int halfhourtimeout) {

        Jedis jedis = null;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(key))
            {

               jedis.expire(key,halfhourtimeout);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
        }
    }
    /**
     *清除key的生存时间
     * @param key
     * @return
     */
    public Long persist (String key) {
        Jedis jedis = null;
        Long result = null;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(key))
            {

                result = jedis.persist(key);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
        }

        return result;
    }

    /**
     * 判断key值是否存在
     * @param key
     * @return
     */
    public Boolean exists(String key ){
        Jedis jedis = null;
        Boolean exists = false;
        try
        {
            jedis = getJedis();
            if (StringUtils.isNotEmpty(key))
            {

                exists = jedis.exists(key);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            returnResource(jedis);
        }
        return exists;
    }




}
